extends PanelContainer

@onready var pwd_le = %pwdLE
@onready var username_le = %usernameLE
@onready var server_le = %serverLE
@onready var msg_label = %msg_label
@onready var remotepath_le = %remotepathLe


func _ready():
	_set_web_dav_data()
	Signalbus.file_uploaded.connect(_on_uploaded)
	Signalbus.file_downloaded.connect(_on_downloaded)
	Signalbus.version_compare_completed.connect(_on_compare_completed)
	

func _set_web_dav_data():
	server_le.text = GlobalVariables.settingData.webdavServerURL
	username_le.text = GlobalVariables.settingData.webdavUsernamme
	pwd_le.text = GlobalVariables.settingData.webdavPassword
	remotepath_le.text = GlobalVariables.settingData.webdavRemoteFolderPath

# 从UI中获取webdav数据
func _validate_data()->bool:
	if GlobalVariables.settingData.webdavServerURL == '':
		msg_label.text = '服务器名不能为空！'
		msg_label.show()
		return false
		
	if GlobalVariables.settingData.webdavUsernamme == '':
		msg_label.text = '用户名不能为空'
		msg_label.show()
		return false
	
	if GlobalVariables.settingData.webdavPassword == '':
		msg_label.text = '密码不能为空'
		msg_label.show()
		return false
	
	if not GlobalVariables.settingData.webdavRemoteFolderPath.ends_with('/'):
		msg_label.text = '远程目录需要以/结尾'
		msg_label.show()
		return false
	
	return true

func _on_server_le_text_submitted(new_text):
	GlobalVariables.settingData.webdavServerURL = new_text

func _on_username_le_text_submitted(new_text):
	GlobalVariables.settingData.webdavUsernamme = new_text

func _on_pwd_le_text_submitted(new_text):
	GlobalVariables.settingData.webdavPassword = new_text

func _on_remotepath_le_text_changed(new_text):
	GlobalVariables.settingData.webdavRemoteFolderPath = new_text

func _on_async_button_pressed():
	if _validate_data():
		GlobalVariables.send_version_compare()

func _on_check_button_pressed():
	if _validate_data():
		GlobalVariables.send_validate_request()
		await Signalbus.webdav_server_validated
		if GlobalVariables.settingData.webdavValidated:
			msg_label.text = '服务器测试成功！'
			msg_label.show()
		else:
			msg_label.text = '服务器测试失败！'
			msg_label.show()

func _on_uploaded():
	msg_label.text = '上传成功'
	msg_label.show()

func _on_downloaded():
	msg_label.text = '下载成功'
	msg_label.show()

func _on_download_btn_pressed():
	if _validate_data():
		GlobalVariables.send_download_request()
		msg_label.show()
		msg_label.text = '服务器配置正覆盖本地配置...'

func _on_upload_btn_pressed():
	if _validate_data():
		GlobalVariables.send_upload_request()
		msg_label.show()
		msg_label.text = '本地配置覆盖服务器配置...'

func _on_compare_completed(state:GlobalVariables.VersionState):
	
	if state == GlobalVariables.VersionState.SAME:
		msg_label.text = '当前版本与服务器版本一致'
		msg_label.show()
	elif state == GlobalVariables.VersionState.FORWARD:
		#GlobalVariables.send_upload_request()
		msg_label.show()
		msg_label.text = '当前版本领先于服务器版本，正在上传'
	elif state == GlobalVariables.VersionState.BACKWARD:		
		#GlobalVariables.send_download_request()
		msg_label.show()
		msg_label.text = '当前版本落后于服务器版本，正在下载'
		
func _on_async_btn_pressed():
	if _validate_data():
		GlobalVariables.send_version_compare()
		msg_label.show()
		msg_label.text = '比较服务器进度...'
